﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows;
using System.Linq;
using OfficeOpenXml;

namespace ToolExcelPub
{
    public static partial class XTool
    {
        public static void OutCsharpEnum()
        {
            if (!OutputCsharpNeed) return;

            ShowInfo("OutCsharpEnum!");

            string pathServer = PathCsharp + "\\" + "ConfSvEnum.cs";
            string pathClient = PathCsharp + "\\" + "ConfClientEnum.cs";

            var sbw = new StringBuilder();

            sbw.AppendLine(NoteGenerated);
            sbw.AppendLine($"using System.Collections.Generic;");
            sbw.AppendLine(NamespaceServer);
            sbw.AppendLine($"{{");

            Dictionary<string, Dictionary<string, string>> DictDictEnums = new Dictionary<string, Dictionary<string, string>>();
            Dictionary<string, string> DictDictEnumsDesc = new Dictionary<string, string>();
            foreach (var kvp in DictDictEnumPub)
            {
                DictDictEnums[kvp.Key] = kvp.Value;
                DictDictEnumsDesc[kvp.Key] = DictDictEnumPubDesc[kvp.Key];
            }
            foreach (var kvp in DictDictEnumPageId)
            {
                DictDictEnums[kvp.Key] = kvp.Value;
            }
            foreach (var kvp in DictDictEnumPage)
            {
                DictDictEnums[kvp.Key] = kvp.Value;
                DictDictEnumsDesc[kvp.Key] = DictDictEnumPubDesc[kvp.Key];
            }
            foreach (var kvp in DictDictEnums)
            {
                var sbnode = new StringBuilder();
                if (DictDictEnumsDesc.TryGetValue(kvp.Key, out var enumdesc))
                {
                    sbnode.AppendLine($"\t//{enumdesc}");
                }
                sbnode.AppendLine($"\tpublic enum E{kvp.Key}");
                sbnode.AppendLine($"\t{{");
                long min = 0, max = 0;
                foreach (var kvp2 in kvp.Value)
                {
                    sbnode.AppendLine($"\t\t{kvp2.Key} = {kvp2.Value},");
                    long.TryParse(kvp2.Value, out long tempmax);
                    min = Math.Min(min, tempmax);
                    max = Math.Max(max, tempmax);
                }
                sbnode.AppendLine($"\t}}");
                sbw.Append(sbnode);
            }
            sbw.AppendLine($"}}");

            //服务器
            {
                ToolGlobal.WriteNewFile(pathServer, sbw.ToString());
            }
            //客户端
            {
                var sbwc = sbw.Replace(NamespaceServer, NamespaceClient);
                ToolGlobal.WriteNewFile(pathClient, sbwc.ToString());
            }
        }
    }
}
